//
//  KAPushView.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import UIKit

class KAPushView: UIView {
    
    var BLOCK_doneBlock : ((UIButton) -> ())?
    
    var backTapEnable: Bool = true
    
    var var_tap: UITapGestureRecognizer?

    lazy var var_backgroundView: UIVisualEffectView = {
        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
        let var_backgroundView = UIVisualEffectView(frame: bounds)
        var_backgroundView.effect = blurEffect
        var_backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        var_tap =  UITapGestureRecognizer(target: self, action: #selector(ka_backViewClick))
        var_backgroundView.addGestureRecognizer(var_tap!)
        return var_backgroundView
    }()
    
    @objc func ka_backViewClick() {
        if backTapEnable == false {
            return
        }
        ka_dismiss()
    }
    
    lazy var var_shadowView: UIView = {
        let var_shadowView = UIView()
        var_shadowView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        var_shadowView.layer.shadowRadius = 23.0
        var_shadowView.layer.shadowOpacity = 1.0
        var_shadowView.layer.shadowColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1).cgColor
        var_shadowView.frame = CGRectMake(0, self.bounds.size.height,self.bounds.size.width, 293)
        return var_shadowView
    }()
    
    
    lazy var var_contentView: UIView = {
        let var_contentView = UIView()
        var_contentView.frame = var_shadowView.bounds
        var_contentView.backgroundColor = .color66748A
        return var_contentView
    }()
    
    lazy var var_line: UIView = {
        let var_line = UIView()
        var_line.backgroundColor = UIColor(0xFFFFFF, alphaVaule: 0.15)
        return var_line
    }()

    lazy var var_closeButton: UIButton = {
        let var_closeButton = UIButton()
        var_closeButton.titleLabel?.font = UIFont.systemFont(ofSize: 18.0)
        var_closeButton.setTitleColor(.black, for: .normal)
        var_closeButton.setTitle("Cancel", for: .normal)
        var_closeButton.addTarget(self, action: #selector(ka_closeAction), for: .touchUpInside)
        return var_closeButton
    }()

    
    lazy var var_doneButton: UIButton = {
        let var_doneButton = UIButton()
        var_doneButton.titleLabel?.font = UIFont.systemFont(ofSize: 18.0)
        var_doneButton.setTitleColor(.black, for: .normal)
        var_doneButton.setTitle("OK", for: .normal)
        var_doneButton.addTarget(self, action: #selector(ka_doneAction(button:)), for: .touchUpInside)
        return var_doneButton
    }()
    
    lazy var var_titleLabel: UILabel = {
        let var_titleLabel = UILabel()
        var_titleLabel.textAlignment = .center
        var_titleLabel.font = UIFont.systemFont(ofSize: kscale(16))
        var_titleLabel.textColor = .black
        return var_titleLabel
    }()
    
    
    @objc func ka_closeAction() {
        ka_dismiss()
    }
    
    @objc func ka_doneAction(button: UIButton) {
        BLOCK_doneBlock?(button)
        ka_dismiss()
    }
    
    @objc func ka_show() {
        alpha = 1.0
        let var_rootWindow : UIWindow = UIApplication.shared.keyWindow!
        var_rootWindow.addSubview(self)
        var_backgroundView.alpha = 0

        UIView.animate(withDuration: 0.25, delay: 0.0, animations: { [self] in
            var_backgroundView.alpha = 1
            var contentViewRect = var_shadowView.frame
            contentViewRect.origin.y = bounds.size.height - contentViewRect.size.height;
            var_shadowView.frame = contentViewRect;
        })
    }
    
    @objc func ka_dismiss() {
        endEditing(true)
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveLinear, animations: { [self] in
            var contentViewRect = var_shadowView.frame
            contentViewRect.origin.y = bounds.size.height
            var_shadowView.frame = contentViewRect
        }, completion: { [self] _ in
            alpha = 0
            removeFromSuperview()
        })
    }

    
    //MARK: - init
    convenience init() {
        self.init(frame: CGRect(x: 0, y: 0, width: var_screenWidth, height: var_screenHeight))
      
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ka_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ka_setUI() {
        frame = UIScreen.main.bounds
        addSubview(var_backgroundView)
        addSubview(var_shadowView)
        var_shadowView.addSubview(var_contentView)
        var_contentView.addSubview(var_doneButton)
        var_contentView.addSubview(var_closeButton)
        var_contentView.addSubview(var_titleLabel)
        var_contentView.addSubview(var_line)
        ka_initSubViews()
        
        let maskPath = UIBezierPath(roundedRect: var_contentView.bounds,
                                    byRoundingCorners: [.topLeft, .topRight],
                                    cornerRadii: CGSize(width: 13, height: 13))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = var_contentView.bounds
        maskLayer.path = maskPath.cgPath
        var_contentView.layer.mask = maskLayer
    }
    
    func ka_initSubViews() {
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var_closeButton.snp.makeConstraints { make in
            make.height.equalTo(22.0);
            make.width.equalTo(60.0)
            make.top.equalTo(6.5);
            make.left.equalTo(6.5);
        }
        
        let label = UILabel()
        label.text = var_doneButton.titleLabel?.text
        let fitWidth = label.sizeThatFits(CGSize(width: Double(MAXFLOAT), height: 22.0)).width
        var_doneButton.snp.makeConstraints { make in
            make.centerY.equalTo(var_closeButton);
            make.right.equalTo(-10.0);
            make.height.equalTo(22.0);
            make.width.equalTo(fitWidth+16);
        }
        
        var_titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(var_closeButton);
            make.left.equalTo(var_closeButton.right).offset(10.0);
            make.right.equalTo(var_doneButton.left).offset(-10.0);
        }
        
        var_line.snp.makeConstraints { make in
            make.left.right.equalTo(var_contentView)
            make.height.equalTo(1)
            make.top.equalTo(var_contentView).offset(40)
        }
    }
}
